//
//  CollectiTVC.m
//  WatchBall
//
//  Created by lanou3g on 15/6/8.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//
#import "CollectionButton.h"
#import "CollectiTVC.h"
#import "DatabaseHandle.h"
#import "MatchCell.h"
#import "NewsModel.h"
#import "UIImageView+WebCache.h"
#import "VideoTableViewCell.h"
#import "RESideMenu.h"
#import "NewsModel.h"
#import "MatchVC.h"
#import "VideoViewController.h"
#import "WebViewController.h"
#import "NewsDetailsModel.h"
#import "NewsTableViewCell.h"
#import "NewsModle.h"
#import "NewsaDetailsViewController.h"

@interface CollectiTVC ()
@property (nonatomic,strong) NSArray *headArray;
@property (nonatomic,assign) CGFloat MatchHeight;
@property (nonatomic,assign) CGFloat videoHeight;
@property (nonatomic,assign) CGFloat newsHeight;
@property (nonatomic,strong) NSMutableArray *viewArray;
@property (nonatomic,strong) CollectionButton *matchButton;
@property (nonatomic,strong) CollectionButton *videoButton;
@property (nonatomic,strong) CollectionButton *newsButton;
@end

@implementation CollectiTVC

- (void)viewDidLoad {
    [super viewDidLoad];
  
    
   
    self.headArray = @[@"新闻",@"比赛",@"视频"];
   
    [self setBarButtonItems];
    [self setSourceArray];
    
    self.viewArray = [NSMutableArray array];
    [self setupViewArray];
 
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:72/255.0 green:105/255.0 blue:141/255.0 alpha:1.0];
}
- (void)setBarButtonItems{
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Back"] style:UIBarButtonItemStylePlain target:self action:@selector(didClickBack)];
    
    self.navigationItem.leftBarButtonItem = barButton;
    barButton.tintColor =[UIColor whiteColor];

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
}

- (void)didClickBack{
   // [self removeFromParentViewController];
    [self.sideMenuViewController presentLeftMenuViewController];
}

- (void)setSourceArray{
    DatabaseHandle *dataHandle = [DatabaseHandle shardInstnce];
    dataHandle.fileName = @"newsDetails.sqlite3";
    
    self.newsCollectionArray =[NSMutableArray arrayWithArray:[dataHandle selectAllModel]];
  
    
    
    dataHandle.fileName = @"match.sqlite3";
    dataHandle.viewController = self;
    self.matchCollectionArray = [NSMutableArray arrayWithArray:[dataHandle selectAllModel]];
  
    
    dataHandle.fileName = @"video.sqlite3";
    self.videoCollectionArray = [NSMutableArray arrayWithArray:[dataHandle selectAllModel]];
  
}


- (void)setupViewArray{
    self.newsButton = [[CollectionButton alloc]initWithFrame:CGRectMake(10,0,CGRectGetWidth([UIScreen mainScreen].bounds)-20, 40)];
    self.newsButton.tintColor = [UIColor whiteColor];
    self.newsButton.imageView.tintColor = [UIColor whiteColor];
   
    self.matchButton = [[CollectionButton alloc]initWithFrame:CGRectMake(10,0,CGRectGetWidth([UIScreen mainScreen].bounds)-20, 40)];
    self.videoButton = [[CollectionButton alloc]initWithFrame:CGRectMake(10,0,CGRectGetWidth([UIScreen mainScreen].bounds)-20, 40)];
    
    NSArray *array = @[_newsButton,_matchButton,_videoButton];
    for (int i=0; i<3; i++) {
        
 
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 40)];
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 50, 40)];
   
    lable.text = self.headArray[i];
    lable.textColor = [UIColor colorWithRed:1/256.0 green:40/256.0 blue:40/256.0 alpha:1.0];
    CollectionButton *button = array[i];
    button.tintColor = [UIColor whiteColor];
    button.imageView.tintColor = [UIColor whiteColor];
    [button setImage:[UIImage imageNamed:@"up"] forState:(UIControlStateNormal)];
        [button addSubview:lable];
    button.tintColor = [UIColor whiteColor];
        button.imageEdgeInsets = UIEdgeInsetsMake(0, CGRectGetWidth(button.bounds)-60, 0, 15) ;
    [button addTarget:self action:@selector(didClickButton:) forControlEvents:(UIControlEventTouchUpInside)];

    button.tag = i+100;
    view.backgroundColor = [UIColor colorWithRed:72/255.0 green:105/255.0 blue:141/255.0 alpha:1.0];
    [view addSubview:button];
        
    UIView *buttonLine = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(view.bounds)-1, CGRectGetWidth(view.bounds), 1)];
        buttonLine.backgroundColor = [UIColor colorWithRed:40/255.0 green:65/255.0 blue:92/255.0 alpha:1.0];
        
    [view addSubview:buttonLine];
    [self.viewArray addObject:view];
    }
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    switch (section) {
            
        case 0:
        {
           if (!_newsButton.isSwitch) {
                return 0;
               
        }
          
            return self.newsCollectionArray.count;
        }
        case 1:
        {
            if (!_matchButton.isSwitch) {
                return 0;
        }
         
            return self.matchCollectionArray.count;
        }
        case 2:
        {
            
            if (!_videoButton.isSwitch) {
                return 0;
            }
          
            return self.videoCollectionArray.count;
        }
        default:
             return 0;
            
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    UITableViewCell *taCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSLog(@"%ld",indexPath.section);
    switch (indexPath.section) {
        case 0:
        {
            if (!_newsButton.isSwitch) {
                
                return taCell;
                
            }else{
                
                [tableView registerNib:[UINib nibWithNibName:@"NewsTableViewCell" bundle:nil] forCellReuseIdentifier:@"myCell"];
                NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
                
                NSLog(@"%ld",_newsCollectionArray.count);
                NewsModle * newmodle = _newsCollectionArray[indexPath.row];
                cell.title.text = newmodle.title;
                cell.summary.text = newmodle.summary;
                cell.replies.text = newmodle.replies;
                [cell.image sd_setImageWithURL:[NSURL URLWithString:newmodle.img] placeholderImage:[UIImage imageNamed:@"placeHold"]];
                
                return cell;
            }
            
            
        }
        case 1:
        {
            
            if (!_matchButton.isSwitch) {
           
                return taCell;
                
            }else{

            [tableView registerNib:[UINib nibWithNibName:@"MatchCell" bundle:nil] forCellReuseIdentifier:@"matchCell"];
            MatchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"matchCell" forIndexPath:indexPath];
            
            cell.newsModel =self.matchCollectionArray[indexPath.row];
            
            [cell.LeftImageV sd_setImageWithURL:[NSURL URLWithString:cell.newsModel.home_logo] placeholderImage:[UIImage imageNamed:@"placeHold"]];
            
                [cell.rightImageV sd_setImageWithURL:[NSURL URLWithString:cell.newsModel.away_logo] placeholderImage:[UIImage imageNamed:@"placeHold"]];
             return cell;
            }
         }
        case 2:
        {
           
            if (!_videoButton.isSwitch) {
             
                return taCell;
            }else{
          
                [tableView registerNib:[UINib nibWithNibName:@"VideoTableViewCell" bundle:nil] forCellReuseIdentifier:@"videoCell"];
                
                VideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"videoCell" forIndexPath:indexPath];
                VideoModel *model =  _videoCollectionArray[indexPath.row];
                cell.model = model;
                [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed:@"placeHold"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    
                }];
                return cell;
            }
         }
            
        default:
            return nil;
    }
     return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            return _newsHeight;
        case 1:
            return _MatchHeight;
        case 2:
            return _videoHeight;
            
        default:
            return 0;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = _viewArray[section];
    return view;
}



-(void)didClickButton:(CollectionButton*)button{
    
      switch (button.tag) {
        case 100:
          {
              if (!_newsButton.isSwitch) {
                  _newsHeight = 100;
                  [_newsButton setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
                  
                  _newsButton.isSwitch = 1;
                  _newsButton.imageView.tintColor = [UIColor whiteColor];
                  _newsButton.tintColor = [UIColor whiteColor];
                  _newsButton.backgroundColor = [UIColor clearColor];
              }else{
                  
                  _newsHeight =0;
                  [_newsButton setImage:[UIImage imageNamed:@"up"] forState:UIControlStateNormal];
                  _newsButton.isSwitch =0;
              }
             
              [self.tableView reloadData];
              
          }

            break;

        case 101:
        {
         
            if (!_matchButton.isSwitch) {
                _MatchHeight = 145;
                [button setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
                
                button.isSwitch = 1;
            }else{
                
                _MatchHeight =0;
                [_matchButton setImage:[UIImage imageNamed:@"up"] forState:UIControlStateNormal];
                _matchButton.isSwitch =0;
            }
                [self.tableView reloadData];

    
       }
            break;
            case 102:
          {
              if (!_videoButton.isSwitch) {
                  _videoHeight = 85;
                  [_videoButton setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
                  
                  _videoButton.isSwitch = 1;
              }else{
                  
                 _videoHeight =0;
                  [_videoButton setImage:[UIImage imageNamed:@"up"] forState:UIControlStateNormal];
                  _videoButton.isSwitch =0;
              }

              [self.tableView reloadData];
          }
              break;
        default:
            break;
    }
}


#pragma ---删除活动收藏---

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        DatabaseHandle *handle = [DatabaseHandle shardInstnce];
        switch (indexPath.section) {
            case 0:
            {
                handle.fileName = @"newsDetails.sqlite3";
                handle.viewController = self;
                NewsModle * model = _newsCollectionArray[indexPath.row];
                [_newsCollectionArray removeObject:model];
                [handle deleteModelByID:model.ID];
                
                [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

            }
                break;
                
            case 1:
            {
               
                handle.fileName = @"match.sqlite3";
                handle.viewController = self;
                NewsModel *model = _matchCollectionArray[indexPath.row];
                [_matchCollectionArray removeObject:model];
                [handle deleteModelByID:model.ID];
                
                [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            }
                break;
            case 2:
            {
                
                handle.fileName = @"video.sqlite3";
                handle.viewController = self;
                VideoModel *video = _videoCollectionArray[indexPath.row];
                [_videoCollectionArray removeObject:video];
                [handle deleteModelByID:video.ID];
                [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            }
                break;
                default:
                break;
        }
        
        
    }
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.section) {
        case 0:
        {
            NewsaDetailsViewController * newsDtailsVC = [[NewsaDetailsViewController alloc] init];
            
            NewsModle * newsModle = _newsCollectionArray[indexPath.row];
            
            newsDtailsVC.newsModle = newsModle;
            
            
            
            newsDtailsVC.replies = newsModle.replies;
            
            newsDtailsVC.newsModle.detailWeb =newsModle.detailWeb;
            
            [self.navigationController pushViewController:newsDtailsVC animated:YES];
        }
            break;
        case 1:
        {
            NewsModel *model = _matchCollectionArray[indexPath.row];
            MatchVC *matchViewC = [[MatchVC alloc]init];
            matchViewC.detailWeb = model.detailWeb;
            
            matchViewC.view.backgroundColor = [UIColor blueColor];
            matchViewC.model = model;
       
            self.navigationController.navigationBar.translucent = NO;
            [self.navigationController pushViewController:matchViewC animated:YES];

        }
            break;
        case 2:
        {
            VideoModel *model = _videoCollectionArray[indexPath.row];
            WebViewController *webVC =[[WebViewController alloc] initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]];
            
            webVC.titleName = model.titleText;
            webVC.model =model;
            UINavigationController *naV = [[UINavigationController alloc]initWithRootViewController:webVC];
            
            webVC.Url =model.fromurl;
            [self presentViewController:naV animated:YES completion:nil];

            
        }
            break;
        default:
            break;
    }
    
    
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
